package com.easyj.jackson2;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class JackonDeserizer {
	private ObjectMapper baseMapper;
	public void init(){
		baseMapper = new ObjectMapper();
        // 对于空的对象转json的时候不抛出错误
        baseMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        // 禁用遇到未知属性抛出异常
        baseMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        baseMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        // 低层级配置
        baseMapper.configure(JsonParser.Feature.ALLOW_COMMENTS,true);
        baseMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES,true);
        baseMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES,true);
        baseMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        
        SimpleModule module = new SimpleModule();
        module.addDeserializer(String.class, new StringDeserializer());

        // 注册module
        baseMapper.registerModule(module);
	}  
	
	   public void testReadValue() throws IOException {
	        String json = "{\n" +
	                "  \"name\" : \"发如雪\",\n" +
	                "  \"course\" : \" \",\n" +
	                "  \"now\" : \"2015-12-17 17:25:13\",\n" +
	                "  \"sexy\" : \"MEN\"\n" +
	                "}";
	        TestBean testBean = baseMapper.readValue(json,TestBean.class);
	        System.out.println(testBean.toString());
	    }
	 
	public static void main(String[] args) {
		JackonDeserizer test=new JackonDeserizer();
    	test.init();
    	try {
    		test.testReadValue();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
class TestBean{
    private String name;
    private String course;
    private Date now;
    private Sexy sexy;
    public TestBean(){}
    public TestBean(String name,Sexy sexy){
        this.name = name;
        this.sexy = sexy;
        this.now = new Date();
    }
 
    public Date getNow() {
        return now;
    }
 
    public void setNow(Date now) {
        this.now = now;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Sexy getSexy() {
        return sexy;
    }
 
    public void setSexy(Sexy sexy) {
        this.sexy = sexy;
    }
 
    public String getCourse() {
        return course;
    }
 
    public void setCourse(String course) {
        this.course = course;
    }
 
    @Override
    public String toString() {
        return "TestBean{" +
                "name='" + name + '\'' +
                ", course='" + course + '\'' +
                ", now=" + now +
                ", sexy=" + sexy +
                '}';
    }
}
enum Sexy{
    MEN("男","M"),WOMEN("女","W");
    private String text;
    private String code;
 
    private Sexy(String text,String code){
        this.text = text;
        this.code = code;
    }
 
    public String getText() {
        return text;
    }
 
    public String getCode() {
        return code;
    }
 
    @Override
    public String toString() {
        return "{\"text\":\""+getText()+"\",\"code\":\""+getCode()+"\"}";
    }
}
